{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b7817251",
   "metadata": {},
   "source": [
    "# 列表练习 & 初始字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cf85560",
   "metadata": {},
   "source": [
    "## 购物车程序\n",
    "> 1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "> 2、让用户输入商品编号进行商品的购买。\n",
    "> 3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "> 4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ff329e73",
   "metadata": {},
   "outputs": [],
   "source": [
    "goods = [\n",
    "    [\"10001\",\"python课本\",\"88\"],\n",
    "    [\"10002\",\"蜜雪冰城—冰茶\",\"10\"],\n",
    "    [\"10003\",\"iphone13 pro\",\"9999\"],\n",
    "    [\"10004\",\"华为电-跑车\",\"400000\"],\n",
    "    [\"10005\",\"小米手环\",\"199\"]\n",
    "]\n",
    "user_shop_list = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17becd9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "支付宝余额 = input(\"请输入您的余额：\")\n",
    "info = '''------goods list-------\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "'''%(goods[0],goods[1],goods[2],goods[3],goods[4])\n",
    "print(info)\n",
    "user_input_ID = input(\"请输入商品的ID：\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1c36485c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['10001', 'python课本', '88']\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'user_input_ID' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-32845b582ab3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgood\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[1;31m# 判断商品的id是否正确\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m     \u001b[1;32mif\u001b[0m \u001b[0muser_input_ID\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mgood\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m         \u001b[1;31m# 判断用户的余额是否够用\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgood\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m支付宝余额\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'user_input_ID' is not defined"
     ]
    }
   ],
   "source": [
    "for good in goods:\n",
    "    print(good)\n",
    "    # 判断商品的id是否正确\n",
    "    if user_input_ID == good[0]:\n",
    "        # 判断用户的余额是否够用\n",
    "        if int(good[2]) < int(支付宝余额):\n",
    "            # 把商品加进list\n",
    "            user_shop_list.append(good)\n",
    "            # 扣除支付宝余额\n",
    "            break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ae8fd82",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
